package one.deploy.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;

@Slf4j
public class DefaultActionExecutor implements IActionExecutor {

    private static LogWriter logWriter = new LogWriter();
    private CommandExecuter commandExecuter = new CommandExecuter();
    private IServiceConfigReader serviceConfigReader;

    public DefaultActionExecutor(IServiceConfigReader serviceConfigReader) {
        this.serviceConfigReader = serviceConfigReader;
    }

    @Override
    @Async("asyncServiceExecutor")
    public void deploy(String name, String commandKey) {
        String key = name + ":" + commandKey;
        log.warn("command(key={}) deploy", key);
        logWriter.init(key, "deploy(key={" + key + "}) start...");

        IServiceConfigReader.Action action = serviceConfigReader.getAction(name, commandKey);
        boolean flag = commandExecuter.exe(action.getTxt(), key);
        if (!flag) {
            return;
        }

        logWriter.success(key, "deploy is end!!!");
    }

    @Override
    public String logs(String name, String commandKey) {
        String key = name + ":" + commandKey;
        return logWriter.poll(key);
    }
}
